//
//  VisitorView.swift
//  swiftWeiBoDemo
//
//  Created by zzw on 16/3/15.
//  Copyright © 2016年 zzw. All rights reserved.
//

import UIKit
protocol VisitorViewDelegate :NSObjectProtocol{

    func loginBtnWillClick()
    
    func registerBtnWillClick()

}
class VisitorView: UIView {
    
    weak var delegate:VisitorViewDelegate?
    
    func setupVisitorInfo(isHome:Bool,imageName:String,message:String){
        
        iconImageView.hidden = !isHome;
        
        homeImageV.image = UIImage(named: imageName)
      
        messageLab.text = message
        
        if isHome{
        
        startAnimation()
        
        }
    
    
    }
    
    func loginBtnClick(){
        //        print(__FUNCTION__)
        delegate?.loginBtnWillClick()
    }
    func registerBtnClick(){
        //        print(__FUNCTION__)
        delegate?.registerBtnWillClick()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加子控件
        addSubview(iconImageView)
        addSubview(maskIconImagV)
        addSubview(homeImageV)
        addSubview(messageLab)
        addSubview(loginBtn)
        addSubview(registerBtn)
      
        //布局子控件
        // 2.1背景图标
        iconImageView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        // 2.2遮罩
        maskIconImagV.xmg_Fill(self)
        // 2.3小房子
        homeImageV.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        // 2.4消息文字
        messageLab.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconImageView, size: nil)
        addConstraint(NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        // 2.5注册按钮
        registerBtn.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLab, size: CGSize(width: 100, height: 35), offset: CGPoint(x: 0, y: 20))
        // 2.6登录按钮
        loginBtn.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: messageLab, size: CGSize(width: 100, height: 35), offset: CGPoint(x: 0, y: 20))

        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func startAnimation(){
    //1 创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画属性
        anim.toValue = 2 * M_PI
        anim.duration = 20;
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        
        iconImageView.layer.addAnimation(anim, forKey: nil)
    
    }
    /// 转盘
    private lazy var iconImageView:UIImageView = {
        
        let imageV = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return imageV
        
    }()
    /// 图标
    private lazy var homeImageV: UIImageView = {
        
        let imageV = UIImageView(image: UIImage(named: "visitordiscover_feed_image_home"))
        return imageV
        
    }()
    /// 文本
    private lazy var messageLab : UILabel = {
        let lab = UILabel()
        lab.numberOfLines = 0
        lab.text = "大声大声道"
        return lab
    }()
    /// 登录按钮
    private lazy var loginBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    /// 注册按钮
    private lazy var registerBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action:"registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    private lazy var maskIconImagV: UIImageView = {
    
    let iv = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
        return iv
    
    }()
    
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
